#include "battledialogitems.h"
#include "battledialog.h"
#include <QPainter>
#include "ghostboarditems.h"
#include "ghost.h"
#include <QDebug>

BattleDialogGhostCard::BattleDialogGhostCard(GameDialog* d)
    :GameDialogItem(d)
{
    _batMarker = new BattleDialogGhostMarker(d);
}


QRectF BattleDialogGhostCard::boundingRect() const{
    if(!_ghostDisplay.isNull())
        return QRectF(-_ghostDisplay.width()/2, -_ghostDisplay.height()/2, _ghostDisplay.width(), _ghostDisplay.height());
    else
        return QRectF();
}

void BattleDialogGhostCard::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    if(!_ghostDisplay.isNull())
        painter->drawPixmap(-_ghostDisplay.width()/2, -_ghostDisplay.height()/2, _ghostDisplay);
}

void BattleDialogGhostCard::setGhostDisplay(GhostSlot *s){
    slotPlaced = s;
    ghostData = s->getGhostData();
    if(ghostData!=0)
        _ghostDisplay = QPixmap(ghostData->getImagePath());
}

void BattleDialogGhostCard::exorThis(){
    if(_batMarker->isVisible()){
        slotPlaced->removeGhostCard();
        _ghostDisplay = 0;
        slotPlaced = 0;
    }
}

void BattleDialogGhostCard::OKToKill(){
    if(ghostData!=0){
        if(((BattleDialog*)getDialog())->getResultWindow()->check(ghostData)){
            _batMarker->show();
        }
        else{
            _batMarker->hide();
        }
    }
}

//battle marker
BattleDialogGhostMarker::BattleDialogGhostMarker(GameDialog* d)
    :GameDialogItem(d)
{
    hide();
}


QRectF BattleDialogGhostMarker::boundingRect() const{
    return QRectF(-50, -50, 100, 100);
}

void BattleDialogGhostMarker::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
    QFont f("Garamond", 20);
    painter->setFont(f);
    painter->drawText(boundingRect(), Qt::AlignCenter, "KILL!!!");
}


//battle button
BattleDialogButton::BattleDialogButton(GameDialog *d)
    :GameDialogButton(d)
{

}

QRectF BattleDialogButton::boundingRect() const{
    return QRectF(-WIDTH/2, -HEIGHT/2, WIDTH, HEIGHT);
}

void BattleDialogButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){

}

//start battle button
BattleDialogStartButton::BattleDialogStartButton(GameDialog *d)
    :BattleDialogButton(d)
{

}

void BattleDialogStartButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::black);
    painter->drawText(boundingRect(), Qt::AlignCenter, "Exorcise");
}

void BattleDialogStartButton::mousePressEvent(QGraphicsSceneMouseEvent *event){

    getDialog()->hideStarted();
    ((BattleDialog*)getDialog())->exorGhost();
}

//cancel battle button
BattleDialogCancelButton::BattleDialogCancelButton(GameDialog *d)
    :BattleDialogButton(d)
{

}

void BattleDialogCancelButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::red);
}

//Battle dialog tao button
BattleDialogTaoButton::BattleDialogTaoButton(GameDialog *d)
    :GameDialogButton(d)
{

}

QRectF BattleDialogTaoButton::boundingRect() const{
    return QRectF(-WIDTH/2, -HEIGHT/2, WIDTH, HEIGHT);
}

//battle dialog tao red button
BattleDialogTaoRedButton::BattleDialogTaoRedButton(GameDialog *d)
    :BattleDialogTaoButton(d)
{
}

void BattleDialogTaoRedButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
    painter->drawText(boundingRect(), Qt::AlignCenter, "Red");
}

void BattleDialogTaoRedButton::mousePressEvent(QGraphicsSceneMouseEvent *event){
    ((BattleDialog*)getDialog())->getResultWindow()->increment(RED);
}

//battle dialog tao blue button
BattleDialogTaoBluButton::BattleDialogTaoBluButton(GameDialog *d)
    :BattleDialogTaoButton(d)
{
}

void BattleDialogTaoBluButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
    painter->drawText(boundingRect(), Qt::AlignCenter, "Blue");
}

void BattleDialogTaoBluButton::mousePressEvent(QGraphicsSceneMouseEvent *event){
    ((BattleDialog*)getDialog())->getResultWindow()->increment(BLUE);
}

//battle dialog tao green button
BattleDialogTaoGreButton::BattleDialogTaoGreButton(GameDialog *d)
    :BattleDialogTaoButton(d)
{
}

void BattleDialogTaoGreButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
    painter->drawText(boundingRect(), Qt::AlignCenter, "Green");
}

void BattleDialogTaoGreButton::mousePressEvent(QGraphicsSceneMouseEvent *event){
    ((BattleDialog*)getDialog())->getResultWindow()->increment(GREEN);
}

//battle dialog tao black button
BattleDialogTaoBlaButton::BattleDialogTaoBlaButton(GameDialog *d)
    :BattleDialogTaoButton(d)
{
}

void BattleDialogTaoBlaButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
    painter->drawText(boundingRect(), Qt::AlignCenter, "Black");
}

void BattleDialogTaoBlaButton::mousePressEvent(QGraphicsSceneMouseEvent *event){
    ((BattleDialog*)getDialog())->getResultWindow()->increment(BLACK);
}

//battle dialog tao yellow button
BattleDialogTaoYelButton::BattleDialogTaoYelButton(GameDialog *d)
    :BattleDialogTaoButton(d)
{
}

void BattleDialogTaoYelButton::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::white);
    painter->drawText(boundingRect(), Qt::AlignCenter, "Yellow");
}

void BattleDialogTaoYelButton::mousePressEvent(QGraphicsSceneMouseEvent *event){
    ((BattleDialog*)getDialog())->getResultWindow()->increment(YELLOW);
}

//status ghost
BattleDialogGhostStatus::BattleDialogGhostStatus(GameDialog *d)
    :GameDialogItem(d), ghostData(0)
{

}

QRectF BattleDialogGhostStatus::boundingRect() const{
    return QRectF(-WIDTH/2,-HEIGHT/2, WIDTH, HEIGHT);
}

void BattleDialogGhostStatus::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::blue);
    if(ghostData!=0){
        QString text = "";
        text.append("Red:"+QString::number(ghostData->getResist0())+"\n");
        text.append("Yellow:"+QString::number(ghostData->getResist1())+"\n");
        text.append("Blue:"+QString::number(ghostData->getResist2())+"\n");
        text.append("Green:"+QString::number(ghostData->getResist3())+"\n");
        text.append("Black:"+QString::number(ghostData->getResist4())+"\n");
        painter->drawText(boundingRect(), Qt::AlignCenter, text);
    }
}

void BattleDialogGhostStatus::setGhostData(GhostSlot *g){
    if(g!=0)
        ghostData = g->getGhostData();
}

//result
BattleDialogTaoResult::BattleDialogTaoResult(GameDialog *d)
    :GameDialogItem(d), red(0), blue(0), black(0), yellow(0), green(0), white(0)
{

}

QRectF BattleDialogTaoResult::boundingRect() const{
    return QRectF(-WIDTH/2,-HEIGHT/2, WIDTH, HEIGHT);
}

void BattleDialogTaoResult::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->fillRect(boundingRect(), Qt::blue);
    QString text = "";
    text.append("Red:"+QString::number(red)+"\n");
    text.append("Black:"+QString::number(black)+"\n");
    text.append("Blue:"+QString::number(blue)+"\n");
    text.append("Yellow:"+QString::number(yellow)+"\n");
    text.append("Green:"+QString::number(green)+"\n");
    text.append("White:"+QString::number(white)+"\n");
    painter->drawText(boundingRect(), Qt::AlignCenter, text);
}

void BattleDialogTaoResult::increment(GSColor g){
    switch(g){
    case RED:
        red++;
        break;
    case YELLOW:
        yellow++;
        break;
    case BLUE:
        blue++;
        break;
    case GREEN:
        green++;
        break;
    case BLACK:
        black++;
        break;
    case WHITE:
        white++;
        break;
    }
    update();
    emit resultUpdated();
}

bool BattleDialogTaoResult::check(Ghost *g){
    if(g->getResist0()<=red && g->getResist1()<=yellow && g->getResist2()<=blue && g->getResist3()<=green && g->getResist4()<=black){
        return true;
    }
    else
        return false;
}

